﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class HistoryBillDetailObject
    {
        public static HistoryBillDetail[] GetHistoryBillDetails()
        {
            return Query(null);
        }

        public static HistoryBillDetail GetHistoryBillDetail(int id)
        {
            HistoryBillDetail historyBillDetail = new HistoryBillDetail();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(HistoryBillDetailDesc.billId, ConditionExpress.EQUALS, id);
            builder.QueryTableName = HistoryBillDetailDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable historyBillDetails = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            historyBillDetail.billId                = Convert.ToInt32(historyBillDetails.Rows[0][HistoryBillDetailDesc.billId]);
            historyBillDetail.tableName             = Convert.ToString(historyBillDetails.Rows[0][HistoryBillDetailDesc.tableName]);
            historyBillDetail.TableProperty         = Convert.ToString(historyBillDetails.Rows[0][HistoryBillDetailDesc.TableProperty]);
            historyBillDetail.TableCategory         = Convert.ToString(historyBillDetails.Rows[0][HistoryBillDetailDesc.TableCategory]);
            historyBillDetail.BillTime              = Convert.ToDateTime(historyBillDetails.Rows[0][HistoryBillDetailDesc.BillTime]);
            historyBillDetail.HeadCount             = Convert.ToInt32(historyBillDetails.Rows[0][HistoryBillDetailDesc.HeadCount]);
            historyBillDetail.TotalPrice            = Convert.ToSingle(historyBillDetails.Rows[0][HistoryBillDetailDesc.TotalPrice]);
            historyBillDetail.discount              = Convert.ToInt32(historyBillDetails.Rows[0][HistoryBillDetailDesc.discount]);
            historyBillDetail.Pay                   = Convert.ToSingle(historyBillDetails.Rows[0][HistoryBillDetailDesc.Pay]);
            historyBillDetail.Waiter                = Convert.ToString(historyBillDetails.Rows[0][HistoryBillDetailDesc.Waiter]);
            historyBillDetail.ServiceTips           = Convert.ToSingle(historyBillDetails.Rows[0][HistoryBillDetailDesc.ServiceTips]);
            historyBillDetail.CheckOutMode          = Convert.ToString(historyBillDetails.Rows[0][HistoryBillDetailDesc.CheckOutMode]);



            return historyBillDetail;
        }

        public static HistoryBillDetail[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + HistoryBillDetailDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                HistoryBillDetail[] historyBillDetails = new HistoryBillDetail[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    HistoryBillDetail historyBillDetail = new HistoryBillDetail();

                    historyBillDetail.billId                   = Convert.ToInt32(table.Rows[i][HistoryBillDetailDesc.billId]);
                    historyBillDetail.tableName                = Convert.ToString(table.Rows[i][HistoryBillDetailDesc.tableName]);
                    historyBillDetail.TableProperty            = Convert.ToString(table.Rows[i][HistoryBillDetailDesc.TableProperty]);
                    historyBillDetail.TableCategory            = Convert.ToString(table.Rows[i][HistoryBillDetailDesc.TableCategory]);
                    historyBillDetail.BillTime                 = Convert.ToDateTime(table.Rows[i][HistoryBillDetailDesc.BillTime]);
                    historyBillDetail.HeadCount                = Convert.ToInt32(table.Rows[i][HistoryBillDetailDesc.HeadCount]);
                    historyBillDetail.TotalPrice               = Convert.ToSingle(table.Rows[i][HistoryBillDetailDesc.TotalPrice]);
                    historyBillDetail.discount                 = Convert.ToInt32(table.Rows[i][HistoryBillDetailDesc.discount]);
                    historyBillDetail.Pay                      = Convert.ToSingle(table.Rows[i][HistoryBillDetailDesc.Pay]);
                    historyBillDetail.Waiter                   = Convert.ToString(table.Rows[i][HistoryBillDetailDesc.Waiter]);
                    historyBillDetail.ServiceTips              = Convert.ToSingle(table.Rows[i][HistoryBillDetailDesc.ServiceTips]);
                    historyBillDetail.CheckOutMode             = Convert.ToString(table.Rows[i][HistoryBillDetailDesc.CheckOutMode]);

                    historyBillDetails.SetValue(historyBillDetail, i);
                }

                return historyBillDetails;
            }
            return null;
        }

        public static Int32 Add(HistoryBillDetail historyBillDetail)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(HistoryBillDetailDesc.billId,             historyBillDetail.billId);
            builder.InsertAdd(HistoryBillDetailDesc.tableName,          historyBillDetail.tableName);
            builder.InsertAdd(HistoryBillDetailDesc.TableProperty,      historyBillDetail.TableProperty);
            builder.InsertAdd(HistoryBillDetailDesc.TableCategory,      historyBillDetail.TableCategory);
            builder.InsertAdd(HistoryBillDetailDesc.BillTime,           historyBillDetail.BillTime);
            builder.InsertAdd(HistoryBillDetailDesc.HeadCount,          historyBillDetail.HeadCount);
            builder.InsertAdd(HistoryBillDetailDesc.TotalPrice,         historyBillDetail.TotalPrice);
            builder.InsertAdd(HistoryBillDetailDesc.discount,           historyBillDetail.discount);
            builder.InsertAdd(HistoryBillDetailDesc.Pay,                historyBillDetail.Pay);
            builder.InsertAdd(HistoryBillDetailDesc.Waiter,             historyBillDetail.Waiter);
            builder.InsertAdd(HistoryBillDetailDesc.ServiceTips,        historyBillDetail.ServiceTips);
            builder.InsertAdd(HistoryBillDetailDesc.CheckOutMode,       historyBillDetail.CheckOutMode);

            builder.QueryTableName = HistoryBillDetailDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(HistoryBillDetailDesc.billId, ConditionExpress.EQUALS, id);
            builder.QueryTableName = HistoryBillDetailDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(HistoryBillDetail historyBillDetail)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(HistoryBillDetailDesc.billId,              historyBillDetail.billId);
            builder.UpdateAdd(HistoryBillDetailDesc.tableName,           historyBillDetail.tableName);
            builder.UpdateAdd(HistoryBillDetailDesc.TableProperty,       historyBillDetail.TableProperty);
            builder.UpdateAdd(HistoryBillDetailDesc.TableCategory,       historyBillDetail.TableCategory);
            builder.UpdateAdd(HistoryBillDetailDesc.BillTime,            historyBillDetail.BillTime);
            builder.UpdateAdd(HistoryBillDetailDesc.HeadCount,           historyBillDetail.HeadCount);
            builder.UpdateAdd(HistoryBillDetailDesc.TotalPrice,          historyBillDetail.TotalPrice);
            builder.UpdateAdd(HistoryBillDetailDesc.discount,            historyBillDetail.discount);
            builder.UpdateAdd(HistoryBillDetailDesc.Pay,                 historyBillDetail.Pay);
            builder.UpdateAdd(HistoryBillDetailDesc.Waiter,              historyBillDetail.Waiter);
            builder.UpdateAdd(HistoryBillDetailDesc.ServiceTips,         historyBillDetail.ServiceTips);
            builder.UpdateAdd(HistoryBillDetailDesc.CheckOutMode,        historyBillDetail.CheckOutMode);

            builder.ConditionAdd(HistoryBillDetailDesc.billId, ConditionExpress.EQUALS, historyBillDetail.billId);
            builder.QueryTableName = HistoryBillDetailDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
